import { http, HTTP_200 } from '@/store/api'

const state = {
  source: '',
  rawHTML: '',
  notesList: [],
  htmlList: [],
  createList: []
}

const getters = {
}

const mutations = {
  updateSource (state, payload) {
    state.source = payload
  },
  updateRawHTML (state, payload) {
    state.rawHTML = payload
  },
  updateNotesList (state, payload) {
    state.notesList = payload
  },
  updateHTMLList (state, payload) {
    state.htmlList = payload
  },
  pushCreateList (state, payload) {
    state.createList.push(payload)
  },
  clearCreateList (state, payload) {
    state.createList = []
  }
}

const actions = {
  getMDSource ({ commit }) {
    http.get('/mdlist/bfpni.md/').then(res => {
      if (res.status === HTTP_200) {
        commit('updateSource', res.data.data)
      }
    })
  },
  getMdList ({ commit }) {
    http.get('/mdlist/').then(res => {
      if (res.status === HTTP_200) {
        commit('updateNotesList', res.data.data)
      }
    })
  },
  getRawHTML ({ commit }, payload) {
    http.get(`/html/${payload.filename}`).then(res => {
      if (res.status === HTTP_200) {
        commit('updateRawHTML', res.data.data)
      }
    })
  },
  getHTMLList ({ commit }) {
    http.get('/html/').then(res => {
      if (res.status === HTTP_200) {
        commit('updateHTMLList', res.data.data)
      }
    })
  }
}

export default {
  state,
  getters,
  mutations,
  actions
}
